package com.ruoyi.web.controller.system;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.system.domain.ComplaintCertificate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.OldPeopleType;
import com.ruoyi.system.service.IOldPeopleTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 服务类型Controller
 *
 * @author ruoyi
 * @date 2021-07-05
 */
@RestController
@RequestMapping("/system/Oldtype")
public class OldPeopleTypeController extends BaseController {
    @Autowired
    private IOldPeopleTypeService oldPeopleTypeService;

    /**
     * 查询服务类型列表
     */
//    @PreAuthorize("@ss.hasPermi('system:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(OldPeopleType oldPeopleType) {
        startPage();
        List<OldPeopleType> list = oldPeopleTypeService.selectOldPeopleTypeList(oldPeopleType);
        return getDataTable(list);
    }

    /**
     * 导出服务类型列表
     */
//    @PreAuthorize("@ss.hasPermi('system:type:export')")
    @Log(title = "服务类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OldPeopleType oldPeopleType) {
        List<OldPeopleType> list = oldPeopleTypeService.selectOldPeopleTypeList(oldPeopleType);
        ExcelUtil<OldPeopleType> util = new ExcelUtil<OldPeopleType>(OldPeopleType.class);
        return util.exportExcel(list, "type");
    }

    /**
     * 获取服务类型详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:type:query')")
    @GetMapping(value = "/{tId}")
    public AjaxResult getInfo(@PathVariable("tId") Long tId) {
        return AjaxResult.success(oldPeopleTypeService.selectOldPeopleTypeById(tId));
    }

    /**
     * 新增服务类型
     */
//    @PreAuthorize("@ss.hasPermi('system:type:add')")
    @Log(title = "服务类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(OldPeopleType oldPeopleType, MultipartFile file) {
        oldPeopleTypeService.insertOldPeopleType(oldPeopleType);
        if (null!=file) {
            try {
                String profile = RuoYiConfig.getProfile();
                ComplaintCertificate complaint = new ComplaintCertificate();
                byte[] bytes = file.getBytes();
                //文件夹
                Path path2 = Paths.get(profile + "//tId//" + oldPeopleType.gettId());
                //具体文件
                Path path = Paths.get(profile + "//tId//" + oldPeopleType.gettId() + "//" + file.getOriginalFilename());
                File file1 = path.toFile();

                //创建文件夹
                if (!file1.exists()) {
                    path2.toFile().mkdirs();
                }
                if (file1.isFile()) {
                    return AjaxResult.error("文件上传失败,文件存在");
                } else {
                    Files.write(path, bytes);//创建文件
                    if (true) {
                        oldPeopleType.settUrl("profile/tId/" + oldPeopleType.gettId() + "/" + file.getOriginalFilename());
                        return toAjax(oldPeopleTypeService.updateOldPeopleType(oldPeopleType));
                    } else {
                        if (file1.isFile()) {
                            file1.delete();
                        }
                        return null;
                    }
                }
            } catch (IOException e) {

            }
        }
        return null;
    }

    /**
     * 修改服务类型
     */
//    @PreAuthorize("@ss.hasPermi('system:type:edit')")
    @Log(title = "服务类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(OldPeopleType oldPeopleType, MultipartFile file) {
        if (null!=file) {
            try {
                String profile = RuoYiConfig.getProfile();
                ComplaintCertificate complaint = new ComplaintCertificate();
                byte[] bytes = file.getBytes();
                //文件夹
                Path path2 = Paths.get(profile + "//tId//" + oldPeopleType.gettId());
                //具体文件
                Path path = Paths.get(profile + "//tId//" + oldPeopleType.gettId() + "//" + file.getOriginalFilename());
                File file1 = path.toFile();

                //创建文件夹
                if (!file1.exists()) {
                    path2.toFile().mkdirs();
                }
                if (file1.isFile()) {
                    file1.delete();
                } else {
                    Files.write(path, bytes);//创建文件
                    if (true) {
                        oldPeopleType.settUrl("profile/tId/" + oldPeopleType.gettId() + "/" + file.getOriginalFilename());
                        return toAjax(oldPeopleTypeService.updateOldPeopleType(oldPeopleType));
                    } else {
                        if (file1.isFile()) {
                            file1.delete();
                        }
                        return null;
                    }
                }
            } catch (IOException e) {

            }
        }
        return toAjax(oldPeopleTypeService.updateOldPeopleType(oldPeopleType));
    }

    /**
     * 删除服务类型
     */
//    @PreAuthorize("@ss.hasPermi('system:type:remove')")
    @Log(title = "服务类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tIds}")
    public AjaxResult remove(@PathVariable Long[] tIds) {
        return toAjax(oldPeopleTypeService.deleteOldPeopleTypeByIds(tIds));
    }
}
